package com.banzhi.shop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.banzhi.shop.R;
import com.banzhi.shop.adapter.CommonAdapter;
import com.banzhi.shop.adapter.CommonHeadAdapter;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.entity.OrderManagement.OrderInfo_data;
import com.banzhi.shop.entity.OrderManagement.Order_Data;
import com.banzhi.shop.model.CallBack.CustomOrderCallBack;
import com.banzhi.shop.model.ICusomOrderModel;
import com.banzhi.shop.model.Impl.CustomOrderImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.DateUtil;
import com.banzhi.shop.utils.EndlessRecyclerOnScrollListener;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.banzhi.shop.MyApp.context;
import static com.mob.MobSDK.getContext;

/**
 * 店主客户的销售订单列表
 */
public class CustomOrderActivity extends BaseActivity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener, CustomOrderCallBack {
    /**
     * 返回
     */
    private ImageView ivback;
    /**
     * 传过来的用户id
     */
    private String id;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private CommonHeadAdapter<Order_Data> adapter;
    private ICusomOrderModel model;
    private int page = 1;
    private List<Order_Data> order_datas = new ArrayList<>();
//    private CustomeOrderAdapter adapter;
    /**
     * 判断listview是不是滑到最低端了
     */
    private boolean isbottom;
    private RelativeLayout rlnodata;
    private int posnum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_order);
        Translucent.setTranslucent(this);
        setinits();
        setListeners();
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        model = new CustomOrderImpl();
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        model.getData(page, id, this);
    }

    private void setinits() {
        ivback = (ImageView) findViewById(R.id.iv_back);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl_listview);
        swipeRefreshLayout.setColorSchemeResources(R.color.themeColor);
        swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.colorWhite);
        rlnodata = (RelativeLayout) findViewById(R.id.rl_nodata);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
    }

    private void setListeners() {
        ivback.setOnClickListener(this);
        swipeRefreshLayout.setOnRefreshListener(this);
        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(layoutManager) {
            @Override
            public void onLoadMore() {
                page++;
                LoadingDialogUtils.showProgress(CustomOrderActivity.this, "正在加载,请稍后~");
                model.getData(page, id, CustomOrderActivity.this);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        model.getData(page, id, this);
    }

    /**
     * @param object 获取数据成功
     */
    @Override
    public void ongetDataSuccess(Object object) {
        if (swipeRefreshLayout.isRefreshing()) {
            swipeRefreshLayout.setRefreshing(false);
        }
        final List<Order_Data> data = (List<Order_Data>) object;
        if (page == 1) {
            order_datas.clear();
        } else {
            if (data.size() == 0) {
                return;
            }
        }
        order_datas.addAll(data);

        if (order_datas.size() == 0) {
            rlnodata.setVisibility(View.VISIBLE);
        } else {
            rlnodata.setVisibility(View.GONE);
        }
        setviews();

    }

    private void setviews() {
        if (adapter == null) {
            adapter = new CommonHeadAdapter<Order_Data>(this, R.layout.item_custom_order, order_datas) {
                @Override
                public void convert(ViewHolder holder, final Order_Data data, final int position) {
//                    posnum = position+1;
                    if (ASUtils.isCanUse(data.getCreatetime())) {
                        holder.setText(R.id.tv_time, DateUtil.getTimeZhiData(data.getCreatetime()));
                    }
                    if (ASUtils.isCanUse(data.getGoodsprice())) {
                        holder.setText(R.id.tv_zongjia, NumberUtils.DecimalFormat(data.getGoodsprice()));
                    }
                    if (ASUtils.isCanUse(data.getDispatchprice())) {
                        holder.setText(R.id.tv_yunfei, NumberUtils.DecimalFormat(data.getDispatchprice()));
                    }
                    if ("1".equals(data.getShoperOrder())) {
                        holder.getImageView(R.id.iv_mai).setImageResource(R.mipmap.mai);
                    } else {
                        holder.getImageView(R.id.iv_mai).setImageResource(R.mipmap.mai1);
                    }
                    if ("0".equals(data.getStatus())) {
                        holder.setText(R.id.tv_statu, "待付款");
                    } else if ("1".equals(data.getStatus())) {
                        holder.setText(R.id.tv_statu, "待发货");
                    } else if ("2".equals(data.getStatus())) {
                        holder.setText(R.id.tv_statu, "待收货");
                    } else if ("3".equals(data.getStatus())) {
                        holder.setText(R.id.tv_statu, "已完成");
                    } else {
                        holder.setText(R.id.tv_statu, "已关闭");
                    }
                    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
                    layoutManager.setSmoothScrollbarEnabled(true);
                    layoutManager.setAutoMeasureEnabled(true);
                    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    ((RecyclerView) (holder.getView(R.id.recyclerview))).setLayoutManager(layoutManager);
                    ((RecyclerView) (holder.getView(R.id.recyclerview))).setHasFixedSize(true);
                    ((RecyclerView) (holder.getView(R.id.recyclerview))).setNestedScrollingEnabled(false);
//                    holder.getView(R.id.recyclerview).setClickable(false);
//                    holder.getView(R.id.recyclerview).setPressed(false);
                    CommonAdapter<OrderInfo_data> adapter = new CommonAdapter<OrderInfo_data>(context, R.layout.item_order_management_shangping, data.getOrderGoods()) {
                        @Override
                        public void convert(ViewHolder holder, OrderInfo_data orderInfodata, int position) {
                            holder.setImageURI(R.id.iv_image, orderInfodata.getThumb());
                            holder.setText(R.id.tv_title, orderInfodata.getTitle());
                            if (orderInfodata.getOptionname() != null && !"".equals(orderInfodata.getOptionname())) {
                                holder.getTextView(R.id.tv_text_guige).setVisibility(View.VISIBLE);
                                holder.setText(R.id.tv_guige, orderInfodata.getOptionname());
                            } else {
                                holder.getTextView(R.id.tv_text_guige).setVisibility(View.GONE);
                            }
                            if (ASUtils.isCanUse(orderInfodata.getPrice())) {
                                holder.setText(R.id.tv_danjia, NumberUtils.DecimalFormat(orderInfodata.getPrice()));
                            }
                            holder.setText(R.id.tv_num, orderInfodata.getTotal());
                            if (orderInfodata.getCommission1() != null && !"".equals(orderInfodata.getCommission1())) {
                                holder.setText(R.id.tv_commission, NumberUtils.DecimalFormat(orderInfodata.getCommission1()));
                            }
                            if ("1".equals(data.getShoperOrder())) {
                                holder.getImageView(R.id.iv_shen_zhuan).setImageResource(R.mipmap.sheng);
                            } else {
                                holder.getImageView(R.id.iv_shen_zhuan).setImageResource(R.mipmap.zhuan);
                            }

                        }
                    };
                    ((RecyclerView) (holder.getView(R.id.recyclerview))).setAdapter(adapter);
                    adapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(View view, int posiion) {
                            Intent osintent = new Intent(CustomOrderActivity.this, OrderDetailsActivity.class);
                            osintent.putExtra("id", order_datas.get(position).getId());
                            osintent.putExtra("status", order_datas.get(position).getStatus());
                            osintent.putExtra("shoperOrder", order_datas.get(position).getShoperOrder());
                            startActivity(osintent);
                        }
                    });
                }

                @Override
                public void headconvert(HeaderViewHolder holder) {

                }
            };
            recyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
        adapter.setOnItemClickListener(new CommonHeadAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent osintent = new Intent(CustomOrderActivity.this, OrderDetailsActivity.class);
                osintent.putExtra("id", order_datas.get(position).getId());
                osintent.putExtra("status", order_datas.get(position).getStatus());
                osintent.putExtra("shoperOrder", order_datas.get(position).getShoperOrder());
                startActivity(osintent);
            }
        });
    }

    /**
     * @param object 获取数据失败
     */
    @Override
    public void ongetDataError(Object object) {
        if (swipeRefreshLayout.isRefreshing()) {
            swipeRefreshLayout.setRefreshing(false);
        }
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }
}
